// appsplus_jinglian/pages/my/my.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    wx.getStorage({
      key: 'jl_user',
      success: function (res) {
        console.log(res)
        that.setData({
          user: res.data
        })
      },
    })

    
    this.gettle();
    
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  gotoweb:function(){
    
  },
  userinfohandel:function(e){
    var code = this.data.code;
    var that = this;
    var data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      code: code
    };
    wx.login({
      success: function (res) {
        var code = res.code
        console.log(res);
        that.setData({
          code: res.code,
        })
        wx.getUserInfo({
          withCredentials: true,
          success: function (res) {
            var userInfo = res.userInfo
            var nickName = userInfo.nickName
            var avatarUrl = userInfo.avatarUrl
            var gender = userInfo.gender //性别 0：未知、1：男、2：女
            var province = userInfo.province
            var city = userInfo.city
            var country = userInfo.country
            console.log(res)


            app.util.request({
              url: 'entry/wxapp/userInfo',
              data: { code: code, encryptedData: res.encryptedData, iv: res.iv },
              success: function (ret) {
                console.log(ret)
                var res = ret.data
                if (res.status){
                  that.setData({
                    user: res.data
                  })
                  wx.setStorage({
                    key: 'jl_user',
                    data: res.data,
                  })
                  global.reloadFlag = 0;
                  global.user = res.data;
                }else{
                  wx.showToast({
                    title: res.msg,
                  })
                }
                
              }
            })
          }
        })

      }
    })


    
    
  },
  goto_about:function(e){
    wx.navigateTo({
      url: './../about/about',
    })
  },
  goto_order_record:function(e){
    if (this.data.user){
      wx.navigateTo({
        url: './../order_record/order_record',
      })
    }else{
      app.util.message('请先登录！', '', 'err_msg')
    }
    
  },
  goto_consume:function(e){
    if(this.data.user){
      wx.navigateTo({
        url: './../consume/consume',
      })
    }else{
      app.util.message('请先登录！','','err_msg')
    }
    
  },
  logout:function(e){
    var that = this
    wx.clearStorage();
    setTimeout(function(){
      that.setData({
        user:false
      })
    },500)
  },
  gettle:function(e){
    var that = this;
    app.util.request({
      url:'entry/wxapp/hotline',
      data:{

      },
      success:function(res){
          var ret = res.data;
          if(ret.status){
            that.setData({
              telnum: ret.data.phone
            })
          }
          
      }
    })
  },
  // 拨打客服电话
  call:function(e){
    var that =this;
    if(this.data.telnum){
      var tel = that.data.telnum;
      wx.makePhoneCall({
        phoneNumber: tel //仅为示例，并非真实的电话号码
      })
    }else{
      app.util.message("暂未获取到客服联系方式",'','err_msg')
    }
    
  }
})


